package com.example.backed.controller;

import com.example.backed.common.ApiResponse;
import com.example.backed.dto.CategoryRequest;
import com.example.backed.entity.Category;
import com.example.backed.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品分类管理", description = "商品分类相关接口")
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "创建分类")
    @PostMapping
    public ApiResponse<Category> createCategory(@Valid @RequestBody CategoryRequest request) {
        return ApiResponse.success(categoryService.createCategory(request));
    }

    @Operation(summary = "更新分类")
    @PutMapping("/{id}")
    public ApiResponse<Category> updateCategory(@PathVariable Long id, @Valid @RequestBody CategoryRequest request) {
        return ApiResponse.success(categoryService.updateCategory(id, request));
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return ApiResponse.success("删除成功", null);
    }

    @Operation(summary = "获取分类详情")
    @GetMapping("/{id}")
    public ApiResponse<Category> getCategory(@PathVariable Long id) {
        return ApiResponse.success(categoryService.getCategory(id));
    }

    @Operation(summary = "获取所有分类")
    @GetMapping
    public ApiResponse<List<Category>> listCategories() {
        return ApiResponse.success(categoryService.listCategories());
    }

    @Operation(summary = "获取子分类列表")
    @GetMapping("/{id}/sub")
    public ApiResponse<List<Category>> listSubCategories(@PathVariable Long id) {
        return ApiResponse.success(categoryService.listSubCategories(id));
    }

    @Operation(summary = "更新分类排序")
    @PutMapping("/{id}/sort")
    public ApiResponse<Boolean> updateSort(@PathVariable Long id, @RequestParam Integer sort) {
        return ApiResponse.success(categoryService.updateSort(id, sort));
    }
} 